package consys.social.core;

import consys.social.core.bo.Entity;
import consys.social.core.dao.FollowDaoImpl;
import consys.social.core.service.AuthenticatedUser;
import consys.social.core.service.FollowServiceImpl;
import consys.social.memcached.dao.spymemcached.SpyMemcachedImpl;
import consys.social.nosql.dao.hbase.HBase;
import consys.social.nosql.dao.hbase.HBaseTable;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SimpleFollow extends HttpServlet{
    private static final long serialVersionUID = -3049636220738739954L;

    @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException{

        String [] servers = {"localhost:11211"};
        SpyMemcachedImpl mem = new SpyMemcachedImpl("binary", servers);
        mem.setCacheOperationTimeoutSeconds(2);
        mem.setDefaultExpirationTimeSeconds(3600);

        FollowServiceImpl fs = new FollowServiceImpl();
        FollowDaoImpl fd = new FollowDaoImpl();
        ThreadLocal<HBase> ent = new ThreadLocal<HBase>();
        HBaseTable ht = new HBaseTable("entities");
        ent.set(ht);
        fd.setEntities(ent);
        fd.setMemcached(mem);
        fs.setFollowDao(fd);

        PrintWriter out = response.getWriter();

        Entity a = new Entity(new Double(Math.random()).toString());
        Entity b = new Entity(new Double(Math.random()).toString());
        AuthenticatedUser.setUser(a);
        try {
            fs.follow(a, b);
        } catch (NotAuthorizedException ex) {
        }

        out.println("<html>");
        out.println("<body>");
        out.println(a +" follows "+b);
        out.println("</body>");
        out.println("</html>");

   }
}
